<template>
  <div class="pc-container">

    <!-- 顶部导航区域 -->
    <el-row class="header-container">
      <el-col :span="24" class="header-content">
        <div class="header-actions">
          <el-breadcrumb :separator-icon="ArrowRight">
            <el-breadcrumb-item :to="{ path: '/' }">首页</el-breadcrumb-item>
            <el-breadcrumb-item :to="{ path: '/category/' }">文物主体管理</el-breadcrumb-item>
          </el-breadcrumb>
        </div>

        <!-- 搜索与新增区域 -->
        <div class="search-upload-group">
          <el-input v-model="searchKeyword" placeholder="搜索文物主体名称、简介、等信息" clearable @keyup.enter="handleSearch"
            class="search-input">
            <template #append>
              <el-button :icon="Search" @click="handleSearch">搜索</el-button>
            </template>
          </el-input>
          <el-button v-if="userInfo.role === 'admin'" type="primary" :icon="Plus" @click="goToAdd" class="upload-btn">
            新增文物主体
          </el-button>
        </div>
      </el-col>
    </el-row>


    <!-- 状态显示区域 -->
    <div v-if="componentState === 'loading'" class="state-container loading-state">
      <div class="loading-text">加载中...</div>
    </div>

    <div v-else-if="componentState === 'empty' || categories.length === 0" class="state-container empty-state">
      <el-empty description="暂无类别数据" />
    </div>

    <div v-else-if="componentState === 'error'" class="state-container error-state">
      <el-empty description="加载失败，请重试" image="error">
        <template #bottom>
          <el-button type="primary" @click="reloadData">重试</el-button>
        </template>
      </el-empty>
    </div>

    <div v-else class="table-container">
      <el-table :data="categories" border stripe highlight-current-row :row-key="(row) => row.id" class="relic-table"
        :scroll-x="1600" @row-click="handleRowClick">
        <el-table-column label="序号" width="60" align="center">
          <template #default="scope">
            <span class="clickable-id">
              {{ (currentPage - 1) * pageSize + scope.$index + 1 }}
            </span>
          </template>
        </el-table-column>
        <el-table-column prop="name" label="名称" width="80" align="center" />
        <el-table-column label="主体图片" width="200" align="center">
          <template #default="scope">
            <el-image v-if="scope.row.image_base64" :src="scope.row.image_base64" width="200" height="200" fit="cover"
              @error="handleImageError(scope.row)" :preview-src-list="[scope.row.image_base64]"
              :preview-teleported="true" @click.stop />
            <div v-else class="image-placeholder">无图片</div>
          </template>
        </el-table-column>

        <el-table-column label="简介" min-width="220" align="center">
          <template #default="scope">
            <div class="description-content">
              {{ scope.row.description || '无描述' }}
            </div>
          </template>
        </el-table-column>

        <!-- ✅ 新增字段开始 -->
        <el-table-column prop="construction_age" label="建筑年代" width="120" align="center">
          <template #default="scope">
            {{ scope.row.construction_age || '未填写' }}
          </template>
        </el-table-column>

        <el-table-column prop="construction_type" label="建筑类型" width="120" align="center">
          <template #default="scope">
            {{ scope.row.construction_type || '未填写' }}
          </template>
        </el-table-column>

        <el-table-column prop="layout_orientation" label="布局方位" width="100" align="center">
          <template #default="scope">
            {{ scope.row.layout_orientation || '未填写' }}
          </template>
        </el-table-column>

        <el-table-column prop="protection_level" label="保护等级" width="120" align="center">
          <template #default="scope">
            <el-tag :type="scope.row.protection_level ? 'success' : 'info'" size="small">
              {{ scope.row.protection_level || '未评级' }}
            </el-tag>
          </template>
        </el-table-column>
        <!-- ✅ 新增字段结束 -->

        <el-table-column label="地理位置" min-width="200" align="center">
          <template #default="scope">
            <div class="description-content">
              {{ scope.row.address || '无地址' }}
            </div>
          </template>
        </el-table-column>
        <!-- <el-table-column label="关联文物数" width="120" align="center">
          <template #default="scope">
            <el-tag type="info">
              {{ scope.row.relic_count || 0 }}
            </el-tag>
          </template>
        </el-table-column> -->

        <el-table-column label="创建时间" width="150" align="center">
          <template #default="scope">
            {{ formatDate(scope.row.created_at) }}
          </template>
        </el-table-column>

        <el-table-column label="最后更新" width="150" align="center">
          <template #default="scope">
            {{ scope.row.updated_at ? formatDate(scope.row.updated_at) : '未更新' }}
          </template>
        </el-table-column>

        <el-table-column label="操作" width="100" align="center" fixed="right">
          <template #default="scope">
            <div>
              <el-button v-if="userInfo.role === 'admin'" type="primary" :icon="Edit" size="small"
                @click.stop="goToEdit(scope.row.id)" class="operation-btn">
                编辑
              </el-button>
            </div>
            <div style="margin-top: 10px;">
              <el-button type="success" :icon="Promotion" size="small"
                @click.stop="goToMainPage(scope.row.id, scope.row.name)" class="operation-btn">
                详情
              </el-button>
            </div>
            <div style="margin-top: 10px;">
              <el-button v-if="userInfo.role === 'admin'" type="danger" :icon="Delete" size="small"
                @click.stop="deleteCategory(scope.row.id)" class="operation-btn">
                删除
              </el-button>
            </div>
          </template>
        </el-table-column>
      </el-table>

      <!-- 分页组件 - 与文物列表保持一致 -->
      <div class="pagination-container" v-if="totalPages > 0">
        <el-pagination v-model:current-page="currentPage" v-model:page-size="pageSize" :page-sizes="[1, 5, 10, 20, 50]"
          :total="total" layout="total, sizes, prev, pager, next, jumper" @size-change="handlePageSizeChange"
          @current-change="handlePageChange" />
      </div>
    </div>
  </div>
</template>

<script setup lang="ts">
import { useCategoryPage } from '@/components/composables/useCateGory'
import { Delete, Edit, Plus, Search, Promotion } from '@element-plus/icons-vue'
import { useUser } from '@/components/composables/usePersonalInfo'
const { userInfo } = useUser()
import { ArrowRight } from '@element-plus/icons-vue'
const {
  currentPage,
  pageSize,
  searchKeyword,
  categories,
  total,
  totalPages,
  componentState,
  reloadData,
  handleSearch,
  handleImageError,
  goToEdit,
  deleteCategory,
  goToAdd,
  handlePageSizeChange,
  handlePageChange,
  formatDate,
  goToMainPage,
} = useCategoryPage()
// 跳转方法：携带id参数到mainpage
// 处理整行点击跳转详情页
const handleRowClick = (row: any) => {
  goToMainPage(row.id, row.name)
}
</script>

<style scoped>
/* 完全复用文物列表页的样式，保持视觉一致性 */
.pc-container {
  padding: 20px;
  background-color: #f5f7fa;
  min-height: 100vh;
  box-sizing: border-box;
}

.header-actions {
  display: flex;
  justify-content: left;
  margin-bottom: 20px;
  padding: 10px 0;
}

.page-title {
  margin: 0;
  font-size: 24px;
  color: #2c3e50;
  font-weight: 600;
}

.search-upload-group {
  display: flex;
  gap: 15px;
  align-items: center;
  width: 100%;
  max-width: 800px;
  margin: 10px 0;
}

.search-input {
  flex: 1;
}

.upload-btn {
  white-space: nowrap;
}

.table-container {
  margin-top: 20px;
}

/* 表格样式保持一致 */
.relic-table {
  width: 100%;
  background-color: #fff;
}

/* 图片占位符样式 - 与文物列表保持一致 */
.image-placeholder {
  width: 200px;
  height: 200px;
  display: flex;
  align-items: center;
  justify-content: center;
  background-color: #f5f5f5;
  color: #999;
  border-radius: 4px;
}

/* 描述内容样式 */
.description-content {
  line-height: 1.5;
  word-break: break-word;
  padding: 0 5px;
  max-height: 100px;
  overflow-y: auto;
}

/* 状态容器样式 */
.state-container {
  min-height: 400px;
  display: flex;
  align-items: center;
  justify-content: center;
}

.loading-state .loading-text {
  font-size: 16px;
  color: #666;
}

/* 分页容器样式 */
.pagination-container {
  margin-top: 20px;
  text-align: right;
}

/* 操作按钮样式 */
.operation-btn {
  margin: 0 4px;
}

.relic-table :deep(.el-table__body tr:hover td) {
  cursor: pointer;
  background-color: #ace2fb79 !important;
}
</style>
